﻿#include "function_DetectionElectrode_rect.h"
//OpenCV MatתHalcon HObject

// Procedures 
void detectionElectrodeRect(HObject ho_image, HObject *ho_region_error, HTuple hv_row1,
	HTuple hv_column1, HTuple hv_row2, HTuple hv_column2, HTuple hv_gray_threshold,
	HTuple *hv_ret, HTuple *hv_message, HTuple *hv_area_error)
{

	// Local iconic variables
	HObject  ho_rectangle1_now, ho_ImageReduced, ho_GrayImage;

	// Local control variables
	HTuple  hv_Row, hv_Column;

	GenRectangle1(&ho_rectangle1_now, hv_row1, hv_column1, hv_row2, hv_column2);
	ReduceDomain(ho_image, ho_rectangle1_now, &ho_ImageReduced);
	Rgb1ToGray(ho_ImageReduced, &ho_GrayImage);
	Threshold(ho_GrayImage, &(*ho_region_error), 0, hv_gray_threshold);
	AreaCenter((*ho_region_error), &(*hv_area_error), &hv_Row, &hv_Column);

	(*hv_ret) = 1;
	(*hv_message) = "";
	return;
}


DetectionElectrodeRect::DetectionElectrodeRect(QDialog* parent, QSettings* qsettings, QString section) : QDialog(parent)
{
    m_qsettings = qsettings;
    m_section = section;

    //m_model_image_path = QCoreApplication::applicationDirPath() + "/" + m_model_image_path;

    this->setWindowTitle(("电极检测"));
    QRect desktop_rect = QApplication::desktop()->geometry();
    double ratio_x = desktop_rect.width()/1920.0;
    double ratio_y = desktop_rect.height()/1080.0;

    move(0,0);
    setFixedSize(1920*ratio_x,1010*ratio_y);

    m_view = new MyQGraphicsView(this);
    m_view->setGeometry(660 * ratio_x, 0, 1000 * 1.25 * ratio_x, 800 * 1.25 * ratio_y);

	m_view->m_graphics_shape_rect = new GraphicsShapeRect(100, 100, 100, 100);
	m_view->m_scene->addItem(m_view->m_graphics_shape_rect);
	QRectF qrectf = m_view->m_graphics_shape_rect->boundingRect();
	row1 = (int)qrectf.y();
	column1 = (int)qrectf.x();
	row2 = (int)(qrectf.y() + qrectf.height());
	column2 = (int)(qrectf.x() + qrectf.width());



	connect(m_view->m_graphics_shape_rect, &GraphicsShapeRect::sendMessage, m_view->m_pixmapItem, &MyQGraphicsPixmapItem::receiveMessage);

    QSize control_size(120*ratio_x,40*ratio_y);

    QWidget* p = new QWidget(this);
    p->move(0*ratio_x,0*ratio_y);
    p->setFixedSize(600*ratio_x,800*ratio_y);

    QGridLayout *qgridLayout = new QGridLayout();
    p->setLayout(qgridLayout);

    int index = 0;

    QSize ButtonSize(160 * ratio_x, 40 * ratio_y);

  

    /*m_qlabel_row1_offset = new QLabel();
    m_qlabel_row1_offset->setText("y左缩进");
    m_qlabel_row1_offset->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_row1_offset, index, 0);
    m_qspinbox_row1_offset = new QSpinBox();
    m_qspinbox_row1_offset->setFixedSize(control_size);
    m_qspinbox_row1_offset->setMinimum(0);
    m_qspinbox_row1_offset->setMaximum(100);
    qgridLayout->addWidget(m_qspinbox_row1_offset, index, 1);
    
    index++;
    m_qlabel_row2_offset = new QLabel();
    m_qlabel_row2_offset->setText("y右缩进");
    m_qlabel_row2_offset->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_row2_offset, index, 0);
    m_qspinbox_row2_offset = new QSpinBox();
    m_qspinbox_row2_offset->setFixedSize(control_size);
    m_qspinbox_row2_offset->setMinimum(0);
    m_qspinbox_row2_offset->setMaximum(100);
    qgridLayout->addWidget(m_qspinbox_row2_offset, index, 1);
    
    index++;
    m_qlabel_column1_offset = new QLabel();
    m_qlabel_column1_offset->setText("x左缩进");
    m_qlabel_column1_offset->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_column1_offset,index,0);
    m_qspinbox_column1_offset = new QSpinBox();
    m_qspinbox_column1_offset->setFixedSize(control_size);
    m_qspinbox_column1_offset->setMinimum(0);
    m_qspinbox_column1_offset->setMaximum(100);
    qgridLayout->addWidget(m_qspinbox_column1_offset, index, 1);


    index++;
    m_qlabel_column2_offset = new QLabel();
    m_qlabel_column2_offset->setText("x右缩进");
    m_qlabel_column2_offset->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_column2_offset,index,0);
    m_qspinbox_column2_offset = new QSpinBox();
    m_qspinbox_column2_offset->setFixedSize(control_size);
    m_qspinbox_column2_offset->setMinimum(0);
    m_qspinbox_column2_offset->setMaximum(100);
    qgridLayout->addWidget(m_qspinbox_column2_offset, index, 1);

    index++;
    m_qlabel_column21_offset = new QLabel();
    m_qlabel_column21_offset->setText("中间左缩进");
    m_qlabel_column21_offset->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_column21_offset, index, 0);
    m_qspinbox_column21_offset = new QSpinBox();
    m_qspinbox_column21_offset->setFixedSize(control_size);
    m_qspinbox_column21_offset->setMinimum(0);
    m_qspinbox_column21_offset->setMaximum(100);
    qgridLayout->addWidget(m_qspinbox_column21_offset, index, 1);


    index++;
    m_qlabel_column22_offset = new QLabel();
    m_qlabel_column22_offset->setText("中间右缩进");
    m_qlabel_column22_offset->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_column22_offset, index, 0);
    m_qspinbox_column22_offset = new QSpinBox();
    m_qspinbox_column22_offset->setFixedSize(control_size);
    m_qspinbox_column22_offset->setMinimum(0);
    m_qspinbox_column22_offset->setMaximum(100);
    qgridLayout->addWidget(m_qspinbox_column22_offset, index, 1);

    index++;
    m_qlabel_height_offset = new QLabel();
    m_qlabel_height_offset->setText("高度缩进");
    m_qlabel_height_offset->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_height_offset,index,0);
    m_qspinbox_height_offset = new QSpinBox();
    m_qspinbox_height_offset->setFixedSize(control_size);
    m_qspinbox_height_offset->setMinimum(0);
    m_qspinbox_height_offset->setMaximum(100);
    qgridLayout->addWidget(m_qspinbox_height_offset, index, 1);*/

   /* index++;
    m_qlabel_eror_size_high = new QLabel();
    m_qlabel_eror_size_high->setText("缺角面积阈值");
    m_qlabel_eror_size_high->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_eror_size_high, index, 0);
    m_qspinbox_eror_size_high = new QSpinBox();
    m_qspinbox_eror_size_high->setFixedSize(control_size);
    m_qspinbox_eror_size_high->setMinimum(0);
    m_qspinbox_eror_size_high->setMaximum(1000);
    qgridLayout->addWidget(m_qspinbox_eror_size_high, index, 1);*/
    
	//index++;
	

    m_qlabel_gray_threshold = new QLabel();
    m_qlabel_gray_threshold->setText("缺陷灰度阈值");
    m_qlabel_gray_threshold->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_gray_threshold, index, 0);
    m_qspinbox_gray_threshold = new QSpinBox();
    m_qspinbox_gray_threshold->setFixedSize(control_size);
    m_qspinbox_gray_threshold->setMinimum(1);
    m_qspinbox_gray_threshold->setMaximum(254);
    m_qspinbox_gray_threshold->setReadOnly(true);
    qgridLayout->addWidget(m_qspinbox_gray_threshold, index, 1);
    m_qslider_gray_threshold = new QSlider(Qt::Horizontal);
    m_qslider_gray_threshold->setTickPosition(QSlider::NoTicks);
    m_qslider_gray_threshold->setSingleStep(1);
    m_qslider_gray_threshold->setMinimum(1);
    m_qslider_gray_threshold->setMaximum(254);
    qgridLayout->addWidget(m_qslider_gray_threshold,index,2 );
    index++;

    m_qlabel_eror_size_low = new QLabel();
    m_qlabel_eror_size_low->setText("缺陷面积阈值");
    m_qlabel_eror_size_low->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_eror_size_low, index, 0);
    m_qspinbox_error_size_low = new QSpinBox();
    m_qspinbox_error_size_low->setFixedSize(control_size);
    m_qspinbox_error_size_low->setMinimum(0);
    m_qspinbox_error_size_low->setMaximum(1000);
    qgridLayout->addWidget(m_qspinbox_error_size_low, index, 1);
	index++;

    m_pushbutton_save_parameter = new QPushButton(this);
    m_pushbutton_save_parameter->setFixedSize(control_size);
    m_pushbutton_save_parameter->setText("保存参数");
    connect(m_pushbutton_save_parameter, &QPushButton::clicked, this, &DetectionElectrodeRect::saveParameter);
    qgridLayout->addWidget(m_pushbutton_save_parameter, index,0);
	index++;

	m_pushbutton_show_orign_image = new QPushButton(this);
	m_pushbutton_show_orign_image->setFixedSize(control_size);
	m_pushbutton_show_orign_image->setText("显示原图");
	connect(m_pushbutton_show_orign_image, &QPushButton::clicked, this, [=]() {
		Mat image_show = himageToMat(image_in);
		QImage q_image = matToQImage(image_show);
		m_view->setImage(q_image, "");
		return true;
	});
	qgridLayout->addWidget(m_pushbutton_show_orign_image, index, 0);

    m_pushbutton_detection = new QPushButton(this);
    m_pushbutton_detection->setFixedSize(control_size);
    m_pushbutton_detection->setText("检测");
    connect(m_pushbutton_detection, &QPushButton::clicked, this, &DetectionElectrodeRect::detectionImage);
    qgridLayout->addWidget(m_pushbutton_detection, index,1);
    
    index++;
    m_pushbutton_return = new QPushButton(this);
    m_pushbutton_return->setFixedSize(control_size);
    m_pushbutton_return->setText("退出");
    qgridLayout->addWidget(m_pushbutton_return, index, 0);
    connect(m_pushbutton_return, &QPushButton::clicked, this, [=]() {
        this->hide();
        });

    
    try
    {
        m_proc_detection = new HDevProcedure("DetectionElectrodeRect");
        m_call_detection = new HDevProcedureCall(*m_proc_detection);
    }
    catch (HDevEngineException& hdev_exception)
    {
        //QMessageBox::information(nullptr, "", hdev_exception.Message());
    }


    loadParameter();
    connect(m_qslider_gray_threshold, &QSlider::valueChanged, this, [=](int value) {
            m_qspinbox_gray_threshold->setValue(value);
            saveParameter();
            testImage();
    });
}

DetectionElectrodeRect::~DetectionElectrodeRect()
{
    ;
}

bool DetectionElectrodeRect::detection( HObject image_in, QString& message)
{
  /*  if (m_image.empty())
    {
        message += "检测图像为空 ";
        return false;
    }*/
    try
    {
      
      

		HTuple ret, msg, htuple_area_error;


		detectionElectrodeRect(image_in, &region_defect, row1, column1, row2, column2, gray_defect_threshold, &ret, &msg, &htuple_area_error);
       

        

        
       
        
        
			double area_error = htuple_area_error[0].D();
			if (area_error > area_defect_threshold)
			{
				
				message += "缺陷面积:" + QString::number(area_error);
				return false ;
			}
			else
			{
				/*message += "缺陷面积:" + QString::number(area_error);*/
				return true;
			}
    }
	catch (const HalconCpp::HOperatorException& hdev_exception)
	{
		message = hdev_exception.ErrorMessage();
		return false;
	}
  
    return true;
}


void DetectionElectrodeRect::detectionImage()
{
	Mat image_show = himageToMat(image_in);
	if (image_show.empty())
	{
		//QMessageBox::information(nullptr,"","没有图像");
		return;
	}
    //region = parent()->m_function_blob_match->region_out;
    QString message;
    

	bool state = detection(image_in, message);
	if (!state)
	{
		message += "错误";
	}
	else
	{
		message += "OK";
	}

    
    draw(image_show, image_show);

    QImage q_image = matToQImage(image_show);
    m_view->setImage(q_image, message);
	m_view->setRectPose(row1, column1, row2, column2);
}

void DetectionElectrodeRect::draw(Mat image_in,Mat& image_out)
{
    if(image_in.empty())
        return;
    
    image_out = drawRegionFill(image_out, region_defect, Scalar(0, 0, 255));

    GenEmptyRegion(&region_defect);
}

void DetectionElectrodeRect::loadParameter()
{
    /*row1_offset = m_qsettings->value(m_section + "/" + "row1_offset").toInt();
    column1_offset = m_qsettings->value(m_section + "/" + "column1_offset").toInt();
    row2_offset = m_qsettings->value(m_section + "/" + "row2_offset").toInt();
    column2_offset = m_qsettings->value(m_section + "/" + "column2_offset").toInt();
    column21_offset = m_qsettings->value(m_section + "/" + "column21_offset").toInt();
    column22_offset = m_qsettings->value(m_section + "/" + "column22_offset").toInt();
    height_offset = m_qsettings->value(m_section + "/" + "height_offset").toInt();*/
    //area_unfilled_threshold = m_qsettings->value(m_section + "/" + "area_unfilled_threshold").toInt();
    area_defect_threshold = m_qsettings->value(m_section + "/" + "area_defect_threshold").toInt();
    gray_defect_threshold = m_qsettings->value(m_section + "/" + "gray_defect_threshold").toInt();

   /* m_qspinbox_row1_offset->setValue(row1_offset);
    m_qspinbox_column1_offset->setValue(column1_offset);
    m_qspinbox_row2_offset->setValue(row2_offset);
    m_qspinbox_column2_offset->setValue(column2_offset);

    m_qspinbox_column21_offset->setValue(column21_offset);
    m_qspinbox_column22_offset->setValue(column22_offset);
    m_qspinbox_height_offset->setValue(height_offset);*/
    //m_qspinbox_eror_size_high->setValue(area_unfilled_threshold);
    m_qspinbox_error_size_low->setValue(area_defect_threshold);
	m_qslider_gray_threshold->setValue(gray_defect_threshold);
    m_qspinbox_gray_threshold->setValue(gray_defect_threshold);

	row1 = m_qsettings->value(m_section + "/" + "row1", 100).toInt();
	column1 = m_qsettings->value(m_section + "/" + "column1", 100).toInt();
	row2 = m_qsettings->value(m_section + "/" + "row2", 200).toInt();
	column2 = m_qsettings->value(m_section + "/" + "column2", 200).toInt();

	m_view->setRectPose(row1, column1, row2, column2);
    return ;
}

void DetectionElectrodeRect::saveParameter()
{
   /* row1_offset = m_qspinbox_row1_offset->value();
    column1_offset = m_qspinbox_column1_offset->value();
    row2_offset = m_qspinbox_row2_offset->value();
    column2_offset = m_qspinbox_column2_offset->value();
    column21_offset = m_qspinbox_column21_offset->value();
    column22_offset = m_qspinbox_column22_offset->value();
    height_offset = m_qspinbox_height_offset->value();*/
    //area_unfilled_threshold = m_qspinbox_eror_size_high->value();
    area_defect_threshold = m_qspinbox_error_size_low->value();
    gray_defect_threshold = m_qspinbox_gray_threshold->value();

	QRectF qrectf = m_view->m_graphics_shape_rect->boundingRect();
	row1 = m_view->m_graphics_shape_rect->pos().y() + (int)qrectf.y();
	column1 = m_view->m_graphics_shape_rect->pos().x() + (int)qrectf.x();
	row2 = m_view->m_graphics_shape_rect->pos().y() + (int)(qrectf.y() + qrectf.height());
	column2 = m_view->m_graphics_shape_rect->pos().x() + (int)(qrectf.x() + qrectf.width());
	m_qsettings->setValue(m_section + "/" + "row1", row1);
	m_qsettings->setValue(m_section + "/" + "column1", column1);
	m_qsettings->setValue(m_section + "/" + "row2", row2);
	m_qsettings->setValue(m_section + "/" + "column2", column2);

   /* m_qsettings->setValue(m_section + "/" + "row1_offset", row1_offset);
    m_qsettings->setValue(m_section + "/" + "column1_offset", column1_offset);
    m_qsettings->setValue(m_section + "/" + "row2_offset", row2_offset);
    m_qsettings->setValue(m_section + "/" + "column2_offset", column2_offset);
    m_qsettings->setValue(m_section + "/" + "column21_offset", column21_offset);
    m_qsettings->setValue(m_section + "/" + "column22_offset", column22_offset);
    m_qsettings->setValue(m_section + "/" + "height_offset", height_offset);
    m_qsettings->setValue(m_section + "/" + "area_unfilled_threshold", area_unfilled_threshold);*/
    m_qsettings->setValue(m_section + "/" + "area_defect_threshold", area_defect_threshold);
    m_qsettings->setValue(m_section + "/" + "gray_defect_threshold", gray_defect_threshold);
}

bool DetectionElectrodeRect::showDialog()
{
	Mat image_show = himageToMat(image_in);
	QImage q_image = matToQImage(image_show);
	m_view->setImage(q_image, "");
	return true;
}

void DetectionElectrodeRect::testImage()
{
    QString message;
    detection(image_in,message);

    Mat image_show = himageToMat(image_in);
    image_show  = Mat::zeros(Size(image_show.cols,image_show.rows),CV_8UC3);
    draw(image_show, image_show);

    QImage q_image = matToQImage(image_show);
    m_view->setImage(q_image, message);
}

